
using System;
using System.Collections.Generic;
using DL.Tools;
using Tools.Extensions;

namespace DL.CfgData
{
    public class TaskItemInfo
    {
        public int id;
        public string name;
        public string tips;
        public string pgs;
        public string rwd;

        public int[] progress;
        
        public TaskItemInfo(Dictionary<string, object> dict)
        {
            if (dict == null) return;
            dict.TryGetValue(nameof(id), out var value);
            id = Convert.ToInt32(value);

            dict.TryGetValue(nameof(name), out value);
            name = Convert.ToString(value);

            dict.TryGetValue(nameof(tips), out value);
            tips = Convert.ToString(value);

            dict.TryGetValue(nameof(pgs), out value);
            pgs = Convert.ToString(value);
            
            var pgsSp = new List<int>();
            pgs.Split("/").ForEach(x =>
            {
                pgsSp.Add(int.Parse(x));
            });
            progress = pgsSp.ToArray();
            
            dict.TryGetValue(nameof(rwd),out value);
            rwd = Convert.ToString(value);

        }

        public void Print()
        {
            Debugger.EO_Log($"id：{id}");
        }
    }

    public class TaskCfgData : TextCfgData
    {
        public override string ConfigFileName => "TaskCfgData";

        public Dictionary<int, TaskItemInfo> TaskItems;
        protected override void Load(string cfgDataContent)
        {
            Reset();
            if (string.IsNullOrEmpty(cfgDataContent))
            {
                Debugger.EO_LogError("未读取到Json数据" + ConfigFileName);
                return;
            }

            var list = MiniJson.Json.Deserialize(cfgDataContent) as List<object>;

            foreach (Dictionary<string,object> data in list)
            {
                var item = new TaskItemInfo(data);
                TaskItems.Add(item.id, item);
            }
        }

        private void Reset()
        {
            TaskItems ??= new Dictionary<int, TaskItemInfo>();
            TaskItems.Clear();
        }
    }
}
